﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RIS.TMP.WEB.APP.Models;
using RIS.TMP.WEB.APP.AddMigrations;

namespace RIS.TMP.WEB.APP.Controllers
{
    public class MRSaveRestoreController : Controller
    {
        private AllContext db = new AllContext();

        //
        // GET: /MRSaveRestore/

        public ActionResult Index()
        {
            var mrsaverestores = db.MRSaveRestores.Include(m => m.MRPoint).Include(m => m.MRestoreToRepair).Include(m => m.MRestoreToAdmin).Where(x => x.RecordStatus == "U");
            return View(mrsaverestores.ToList());
        }

        //
        // GET: /MRSaveRestore/Details/5

        public ActionResult Details(int id = 0)
        {
            MRSaveRestore mrsaverestore = db.MRSaveRestores.Find(id);
            if (mrsaverestore == null)
            {
                return HttpNotFound();
            }
            return View(mrsaverestore);
        }

        //
        // GET: /MRSaveRestore/Create

        public ActionResult Create()
        {
            ViewBag.MRPId = new SelectList(db.MRPoints, "MRPId", "RecordStatus");
            ViewBag.ReId = new SelectList(db.MRestoreToRepairs, "ReId", "ReCode");
            ViewBag.ResId = new SelectList(db.MRestoreToAdmins, "ResId", "ResCode");
            return View();
        }

        //
        // POST: /MRSaveRestore/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MRSaveRestore mrsaverestore)
        {
            if (ModelState.IsValid)
            {
                db.MRSaveRestores.Add(mrsaverestore);
                mrsaverestore.RecordStatus = "U";
                mrsaverestore.CreateBy = Session["LogedId"].ToString();
                mrsaverestore.CreateWhen = DateTime.Now;
                if (mrsaverestore.CloseId == 1)
                {
                    var product = (from p in db.MRestoreToAdmins
                                   where p.ResId == mrsaverestore.ResId
                                   select p).Single();

                    product.MRestore = 4;
                    db.SaveChanges();
                }
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.MRPId = new SelectList(db.MRPoints, "MRPId", "RecordStatus", mrsaverestore.MRPId);
            ViewBag.ReId = new SelectList(db.MRestoreToRepairs, "ReId", "ReCode", mrsaverestore.ReId);
            ViewBag.ResId = new SelectList(db.MRestoreToAdmins, "ResId", "ResCode", mrsaverestore.ResId);
            return View(mrsaverestore);
        }

        //
        // GET: /MRSaveRestore/Edit/5

        public ActionResult Edit(int id = 0)
        {
            MRSaveRestore mrsaverestore = db.MRSaveRestores.Find(id);
            if (mrsaverestore == null)
            {
                return HttpNotFound();
            }
            ViewBag.MRPId = new SelectList(db.MRPoints, "MRPId", "RecordStatus", mrsaverestore.MRPId);
            ViewBag.ReId = new SelectList(db.MRestoreToRepairs, "ReId", "ReCode", mrsaverestore.ReId);
            ViewBag.ResId = new SelectList(db.MRestoreToAdmins, "ResId", "ResCode", mrsaverestore.ResId);
            return View(mrsaverestore);
        }

        //
        // POST: /MRSaveRestore/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(MRSaveRestore mrsaverestore)
        {
            if (ModelState.IsValid)
            {
                db.Entry(mrsaverestore).State = EntityState.Modified;
                mrsaverestore.UpdateBy = Session["LogedId"].ToString();
                mrsaverestore.UpdateWhen = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.MRPId = new SelectList(db.MRPoints, "MRPId", "RecordStatus", mrsaverestore.MRPId);
            ViewBag.ReId = new SelectList(db.MRestoreToRepairs, "ReId", "ReCode", mrsaverestore.ReId);
            ViewBag.ResId = new SelectList(db.MRestoreToAdmins, "ResId", "ResCode", mrsaverestore.ResId);
            return View(mrsaverestore);
        }

        //
        // GET: /MRSaveRestore/Delete/5

        public ActionResult Delete(int id = 0)
        {
            MRSaveRestore mrsaverestore = db.MRSaveRestores.Find(id);
            if (mrsaverestore == null)
            {
                return HttpNotFound();
            }
            return View(mrsaverestore);
        }

        //
        // POST: /MRSaveRestore/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            MRSaveRestore mrsaverestore = db.MRSaveRestores.Find(id);
            //db.MRSaveRestores.Remove(mrsaverestore);
            mrsaverestore.RecordStatus = "D";
            mrsaverestore.UpdateBy = Session["LogedId"].ToString();
            mrsaverestore.UpdateWhen = DateTime.Now;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}